package com.xxgc.zde.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxgc.zde.api.WechatLoginApi;
import com.xxgc.zde.api.response.WechatLoginResponse;
import com.xxgc.zde.controller.dto.UserLoginDTO;
import com.xxgc.zde.controller.vo.UserLoginVO;
import com.xxgc.zde.entity.UsersEntity;
import com.xxgc.zde.entity.UsersEntityRepository;
import com.xxgc.zde.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/* ResponseBody + Controller*/
@RestController
@RequestMapping("/login")
@Tag(name = "登录接口")
public class UserLoginController {
    // 1ban
    // 2ban  wx04a68abf112b7e5f
    private static final String AppID = "wx04a68abf112b7e5f";
    // 1ban
    // 2ban  40259ae63e288f5633b636a5348857f5
    private static final String AppSecret = "40259ae63e288f5633b636a5348857f5";

    @Autowired
    private WechatLoginApi wechatLoginApi;
    @Autowired
    private UsersEntityRepository usersEntityRepository;



    @GetMapping("/getMsg")
    public Result getMsg1(){
        return Result.success("获取成功");
    }

    @GetMapping("/getInfo")
    public Result getInfo(){
        return Result.success("获取消息成功");
    }




    //游戏注册登录
    @Operation(summary = "游戏登录")
    @PostMapping("/gameLogin")
    public Result gameLogin(@RequestBody Map map){
        String nickname = (String) map.get("nickname");
        String password = (String) map.get("password");
        //条件构造器
        QueryWrapper<UsersEntity> qw = new QueryWrapper<>();
        qw.eq("nickname",nickname);
        //查询一条
        UsersEntity a = usersEntityRepository.getOne(qw);
        if(a == null){
            //插入数据
            UsersEntity usersEntity = new UsersEntity();
            usersEntity.setNickname(nickname);
            usersEntity.setPassword(password);
            usersEntityRepository.save(usersEntity);
            return Result.success().msg("注册成功").code(11012);
        }else{
            //登录逻辑
            qw.eq("password",password);
            UsersEntity b = usersEntityRepository.getOne(qw);
            if(b == null){
                return Result.fail("用户名或密码错误");
            }else{
                //获取用户登陆的id
                String id = b.getId();
                //加密这个id 保存到会话中
                StpUtil.login(id);
                //获取token
                String tokenValue = StpUtil.getTokenValue();

                return Result.success(tokenValue).msg("欢迎使用");
            }
        }
    }









    @Operation(summary = "用户登录")
    @PostMapping("/userLogin")
    public UserLoginDTO userLogin(@RequestBody UserLoginVO userLoginVO){
        return null;
    }


    @Operation(summary = "微信登录")
    @PostMapping("/wechatLogin/{code}")
    public String wechatLogin(@PathVariable String code){
        System.out.println("code = " + code);
        WechatLoginResponse ac = wechatLoginApi.getUserOpenId(AppID, AppSecret, code, "authorization_code");
        System.out.println("authorization_code = " + ac);

        // 1、查询数据库里面有没有openId
        // 有  获取用户的id
        // 没有 先插入然后获取用户id
        // https://baomidou.com/guides/data-interface/#save
        QueryWrapper<UsersEntity> qw = new QueryWrapper<>();
        // https://baomidou.com/guides/wrapper/#alleq
        qw.eq("open_id",ac.getOpenid());
        UsersEntity one = usersEntityRepository.getOne(qw);
        if(one == null){//没有这个用户 - 注册
            one = new UsersEntity();
            one.setOpenId(ac.getOpenid());
            usersEntityRepository.save(one);
        }
        return one.getId();
    }

}